﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCL.UI.Blocks;
using Baosight.ZNH.ZMCL.UI.PopWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 巡检工单管理
    /// </summary>
    /// Title:ZMCL01,ServiceZMCL01
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 严洪浩
    /// Version: 1.0
    /// History:
    ///       2011-6-8  张小飞   [创建]

    public partial class ZMCL01 : EFViewCmes
    {
        private InspectNoteBlock block = new InspectNoteBlock();//构造前台下拉列表
        private string actionType;//操作类型
        private string noteCode;
        private string inspectionUnit;

        public ZMCL01()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            //设置下拉框block
            initData.AddBlock(block.GetStatusBlock(true));

            //获取新增时的默认值
            noteCode = initData.Get("noteCode").ToString();
            inspectionUnit = initData.Get("inspectionUnit").ToString();

            //初始化查询区
            this.fdStatus.Value = "";
            this.fdNotesource.Value = "";

            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "巡检工单管理-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            this.actionType = "update";
            base.OnUpdateButtonClickPopup(eiInfo, "巡检工单管理-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 删除按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "delete";
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 删除按钮事件的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoDeleteOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if(e.EIInfo.Status != -1)
            {
                noteCode = e.EIInfo.Get("noteCode").ToString();
            }
            base.DoDeleteOperationCompleted(sender, e);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
                this.UpdateHomePageData(new EIInfo());
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 发单按钮的执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBillingButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("billing", "发单", sender, e);
        }

        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "巡检工单管理-查看", "detail", null, sender, e);
        }

       
        /// <summary>
        /// 删除,修改前的判断
        /// </summary>
        /// <param name="rowList"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            bool Flag = true;
            if (this.actionType != null && this.actionType == "update")
            {
                if (!"草拟".Equals(_eIRows[0]["fdStatus"].ToString()))
                {
                    base.OpenEFWindowAlert("该巡检工单已派发，不能修改！", true);
                    Flag = false;
                }
            }
            else if (this.actionType == "delete")
            {
                foreach (var row in _eIRows)
                {
                    if (!"草拟".Equals(row["fdStatus"].ToString()))
                    {
                        base.OpenEFWindowAlert("所选记录中包含已发单的工单，不能删除！", true);
                        Flag = false;
                        break;
                    }
                }
            }
            this.actionType = null;
            return Flag;
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            InspectNote inspectNote = new InspectNote(_dialogType);//构造一个新的修改页面
            inspectNote.ViewDataChanged += new EventHandler<ViewDataChangedEventArgs>(inspectNote_ViewDataChanged);//添加监听

            inspectNote.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.noteSource"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.securityPath"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.responsibilityPeople"));

            if ("insert".Equals(_dialogType))
            {
                inspectNote.fdNotecode.Value = noteCode;
                inspectNote.fdDepartmentId.Value = inspectionUnit;
                inspectNote.fdNotedate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            }
         
            inspectNote.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = inspectNote;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 给弹出窗口页面添加一个监听，为了获取弹出窗口中值改变后的新值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void inspectNote_ViewDataChanged(object sender, ViewDataChangedEventArgs e)
        {
            noteCode = e.NewData.ToString();
        }

    }
}
